package com.lagou.bigdata.task04;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * 编程实现客户端接收线程的功能
 */
public class ClientReceiveThread extends Thread {

    private Socket socket;

    public ClientReceiveThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            while (true) {
                // 由于服务器转发时创建新的对象输出流，因此这里创建新的对象输入流进行关联
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                // 实现接收服务器发来的数据内容并打印，可以通过类型消息类型区分文字和文件
                CommunicationMessage communicationMessage = (CommunicationMessage) objectInputStream.readObject();
                System.out.println(socket.getInetAddress() + "发来的消息是：" + communicationMessage.getContent());

            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}


